package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.dao.FeedbackMapper;
import com.baizhi.entity.Feedback;
import com.baizhi.entity.Log;
import com.baizhi.entity.LogExample;
import com.baizhi.service.FeedbackService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class FeedbackServiceImpl implements FeedbackService {

    @Resource
    FeedbackMapper feedbackMapper;

    @AddCache
    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer rows) {
        //创建返回对象
        HashMap<String, Object> map = new HashMap<>();

        //存入当前页
        map.put("page",page);

        //查总条数
        LogExample example = new LogExample();
        int records = feedbackMapper.selectCountByExample(example);
        //存入总条数
        map.put("records",records);

        //总页数=总条数/每页显示条数、
        Integer total = records/rows==0?records/rows:records/rows+1;
        map.put("total",total);

        //数据
        RowBounds rowBounds = new RowBounds((page-1)*rows,rows);
        List<Feedback> feedbacks = feedbackMapper.selectByExampleAndRowBounds(example, rowBounds);
        map.put("rows",feedbacks);

        return map;
    }
}
